#!/bin/bash

#change directory to script path
curpath=$(cd "$(dirname "$0")"; pwd)
cd $curpath > /dev/null

source /usr/local/sunlogin/scripts/common.sh

#kill all runing sunloginclient
killall sunloginclient > /dev/null 2>&1


if [ $os_name == 'ubuntu' ] || [ $os_name == 'deepin' ] || [ $os_name == 'kylin' ] || [ $os_name == 'debian' ]; then
	echo $os_name > /dev/null 2>&1
else
	echoAndExit 'unknown OS it not impl'
fi
	
os_version_int=${os_version%.*}
for i in $(seq 1 10)
do
	os_version_int=${os_version_int%.*}
done

chmod +x $path_etc/watch.sh
chmod 766 /usr/share/applications/sunloginshell.desktop

#echo "create init"
if [ $os_name == 'ubuntu' ]; then
	if [ $os_version_int -lt 15 ]; then
		cp /usr/local/sunlogin/scripts/runsunloginclient.conf /etc/init/runsunloginclient.conf || echoAndExit 'can not copy init file runsunloginclient.conf'
	else
		cp /usr/local/sunlogin/scripts/runsunloginclient.service /etc/systemd/system/runsunloginclient.service || echoAndExit 'can not copy init file runsunloginclient.service'
		systemctl enable runsunloginclient.service
	fi

	cd - > /dev/null

elif [ $os_name == 'kylin' ]; then
	cp /usr/local/sunlogin/scripts/runsunloginclient.service /etc/systemd/system/runsunloginclient.service || echoAndExit 'can not copy init file runsunloginclient.service'
	systemctl enable runsunloginclient.service

elif [ $os_name == 'debian' ]; then
	cp /usr/local/sunlogin/scripts/runsunloginclient.service /etc/systemd/system/runsunloginclient.service || echoAndExit 'can not copy init file runsunloginclient.service'
	systemctl enable runsunloginclient.service

elif [ $os_name == 'deepin' ]; then

	if [ $os_version_int -gt 2000 ]; then
		let os_version_int=os_version_int-2000
	fi
	if [ $os_version_int -lt 15 ]; then
		cp /usr/local/sunlogin/scripts/runsunloginclient.conf /etc/init/runsunloginclient.conf || echoAndExit 'can not copy init file runsunloginclient.conf'
	else
		cp /usr/local/sunlogin/scripts/runsunloginclient.service /etc/systemd/system/runsunloginclient.service || echoAndExit 'can not copy init file runsunloginclient.service'
		systemctl enable runsunloginclient.service
	fi

	cd - > /dev/null
fi

if [ -f "/tmp/orayconfig.conf" ]; then
	cp /tmp/orayconfig.conf /etc/
	rm /tmp/orayconfig
fi
/usr/local/sunlogin/scripts/start.sh
